<?php
class tag extends Model{
	public static function getTableName(){
		return 'tag';
	}

	public function getByType($typeid)
	{
		$sql = 'select * from tag where tag_type_id = '.((int)$typeid).' and parent_id=0';
		return Model::$dbo['r']->findBySql(__class__,$sql);
	}

	public function getSubTag($parentid, & $json_data)
	{
		$sql = 'select * from tag where parent_id='.((int)$parentid);
		$ret =  Model::$dbo['r']->findBySql(__class__,$sql);

		if (count($ret) == 0)
		{
			$json_data['child'] = array();
			return;
		}
		else
		{
			$j = 0;
			foreach ($ret as $tag)
			{
				$child_data[$j] = array("id"=>$tag->id, "name"=>$tag->system_label);
				$this->getSubTag($tag->id, $child_data[$j]);
				$j++;
			}
			$json_data['child'] = $child_data;
		}
	}

	public function getTagList($instr)
	{
		# changed by vanni
		$tag_ids = array_unique(explode(',',$instr));
		$result  = array();
		foreach($tag_ids as $tag_id){
			$sql = "SELECT a.system_label, a.id, b.id as typeid from tag a left join tag_type b on a.tag_type_id=b.id where a.id = " . (int)$tag_id;
			$tag_result = Model::$dbo['r']->findBySql(__class__, $sql);
			$result[] = $tag_result[0];
		}
		return $result;

		$sql = "select a.system_label as system_label,a.id as id,b.id as typeid from tag a left join tag_type b on a.tag_type_id=b.id where a.id in(".$instr.")";
		return Model::$dbo['r']->findBySql(__class__, $sql);
	}

    /**
     * get proofread SQL param
     * @param   $id Int
     * @return  Array
     */
    public function getProofread($id)
    {
        if (empty($id)) return  array();
        $sqlArr =   array(
                    'tag'   =>  array('id = "'. $id. '"'),
                );

        $sqlArray   =   array();
        foreach ($sqlArr    as  $tableName  =>  $whereArray)
        {
            $sqlArray[$tableName]   =   Model::assembleGetProofreadSql($tableName, $whereArray);
        }
        return  $sqlArray;
    }

	/**
	 * check if exists tag
	 *
	 * @param $tag_type : tag type
	 * @param $tag_name : tag name
	 *
	 * @used by : ChapterUpdateAction.class.php
	 */
	public static function isExist($tag_type, $tag_name)
	{
		#$sql = "select id from tag where tag_type_id = '".$tag_type."' and system_label='".$tag_name."'";
		#$ret = Model::$dbo['r']->findBySql(__class__, $sql);
		#return isset($ret[0]) ? $ret[0]->id : false;

		$sql = 'SELECT id FROM tag WHERE tag_type_id=:tag_type_id AND system_label=:label';
		$ret = Model::selectBySql($sql,array(':tag_type_id'=>$tag_type,':label'=>$tag_name));
		return ($ret ? $ret[0]['id'] : false);
	}

	/**
	 * check node if parent
	 *
	 * @param int $category_id : category id
	 *
	 * @used by : action/ProgrammeGetAction.class.php
	 */
	public static function IsParent($tag_id)
	{
		#$sql = 'select parent_id from '.self::getTableName().' where id=\''.$tag_id.'\' and tag_type_id=1';
		#$ret =  Model::$dbo['r']->findBySql(__class__, $sql);
		#return ($ret[0]->parent_id == 0) ? $tag_id : $ret[0]->parent_id;

		#$sql = 'SELECT parent_id FROM tag WHERE id=:tag_id AND tag_type_id=1';
		$sql = 'SELECT parent_id FROM tag WHERE id=:tag_id';
		$ret = Model::selectBySql($sql,array(':tag_id'=>$tag_id));
		return (!isset($ret[0]) || $ret[0]['parent_id']==0 ? $tag_id : $ret[0]['parent_id']);
	}
    
    public static function getLibTag($tag_id)
	{
		$sql = 'SELECT parent_id FROM tag WHERE id=:tag_id AND tag_type_id=4';
		$ret = Model::selectBySql($sql,array(':tag_id'=>$tag_id));
		return ($ret[0]['parent_id']==0 ? $tag_id : $ret[0]['parent_id'].','.$tag_id);
	}

	public static function isRestricted($tag_type){
		static $all_tag_type;
		if(empty($all_tag_type)){
			$rows = Model::selectBySql('select * from tag_type');
			foreach($rows as $row){
				$all_tag_type[$row['id']] = $row['restricted'];
			}
		}
		return $all_tag_type[$tag_type];
	}

	/*
	 * by donald
	 */
	public function add_change_log()
	{
		$field['type'] = $this->getTableName();
		$field['keys'] = array('id' => $this->id);
		$field['is_active'] = 1;
		$field['programme_id'] = 0;
		$field['episode_id'] = 0;
		$field['is_publish'] = 0;

		change_log::updateOne($field);
	}

}
?>
